// pages/cart/cart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //购物车中是否有数据
    isData: false,
    totalnum: 0,
    list: []

  },
  nag:function(){
    wx.navigateTo({
      url: '/pages/pay/pay'
    })
  },
  isChecked: function (e) {
    let num = 0
    let {id} = e.target.dataset
    let carList = wx.getStorageSync('carList')
    //如果ischeck等于true
    if (this.data.list[id].checked == true) {
      carList[id].checked = false
      wx.setStorageSync('carList', carList)
    }
    if (this.data.list[id].checked == false) {
      carList[id].checked = true
      wx.setStorageSync('carList', carList)
    }
    for (let i = 0; i < carList.length; i++) {
      if (carList[i].checked == true) {
        num +=carList[i].new*carList[i].num
      }
    }
    this.setData({
      totalnum:num,
      list:carList
    })

  },
  toIndex: function () {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  addNum:function(e){
    let num = 0
    let {id} = e.target.dataset
    let carList = this.data.list
    carList[id].num++
    wx.setStorageSync('carList', carList)
    for (let i = 0; i < carList.length; i++) {
      if (carList[i].checked == true) {
        num +=carList[i].new*carList[i].num
      }
    }
    this.setData({
      list:carList,
      totalnum:num
    })
  },
  decNum:function(e){
    let btn = true
    let num = 0
    let {id} = e.target.dataset
    let carList = this.data.list
    if (carList[id].num>0) {
      carList[id].num--
    }
    wx.setStorageSync('carList', carList)
    for (let i = 0; i < carList.length; i++) {
      if (carList[i].checked == true) {
        num +=carList[i].new*carList[i].num
      }
    }
    if (carList[id].num==0) {
      carList.splice(id,1)
    }
    if (carList.length==0) {
      btn = false
    }
    this.setData({
      isData:btn,
    })
  },
  delectSp:function(e){
    let btn = true
    let num = this.data.totalnum
    let {id} = e.target.dataset
    let carList = this.data.list
    num -= carList[id].num*carList[id].new
    carList.splice(id,1)
    wx.setStorageSync('carList', carList)
    if (carList.length==0) {
      btn = false
    }
    this.setData({
      isData:btn,
      list:carList,
      totalnum:num
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let carList = wx.getStorageSync('carList')
    let isData = false
    let num = 0
    if (carList.length != 0) {
      isData = true
    }
    for (let i = 0; i < carList.length; i++) {
      if (carList[i].checked == true) {
        num +=carList[i].new*carList[i].num
      }
    }
    this.setData({
      isData: isData,
      list: carList,
      totalnum:num
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})